import { reqLogin } from '@/api/user/index'
import type { loginFormData, } from '@/api/user/type'
import type { loginResponseData, userStore } from "@/store/modules/type/type"
import { defineStore } from 'pinia'
const useUserStore = defineStore('user', {
  state: (): userStore => ({
    token: '',
  }),
  persist: {
    key: 'token',
    storage: localStorage,
    paths: ['token'], // 明确声明要持久化的字段
  } as any,
  getters: {},
  actions: {
    async userLogin (data: loginFormData) {
      let res: loginResponseData = await reqLogin(data)
      if (res.code === 200) {
        // 登录成功
        this.token = (res.data.token as string)
        return 'ok'
      } else {
        return Promise.reject(new Error(res.data.message || '登录失败'));
      }
    },
  },
})

export default useUserStore
